package com.elegantsoft.pakistannews;

import java.util.ArrayList;
import java.util.HashMap;

import com.atiffarrukh.pakistannews.R;
import com.elegantsoft.pakistannews.rssfeed.MessageList;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CutomBaseAdapter extends BaseAdapter {
	//private Activity activity;
	private ArrayList<HashMap<String, String>> data;
	private static LayoutInflater inflater=null;
	private Context context;
	ImageView image;
	com.elegantsoft.pakistannews.lazylist.ImageLoader  imageLoader;
	
	public CutomBaseAdapter (Context c, ArrayList<HashMap<String, String>> d){
		context = c;
		data = d;
		inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		//context = MessageList.class;
		imageLoader=new com.elegantsoft.pakistannews.lazylist.ImageLoader(c.getApplicationContext());
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		View vi = view;
		//Log.d("news", " before if" );
		//if(view == null)
		vi =inflater.inflate(R.layout.row_cat1, null);
		//Log.d("news", " after inflation" );
		TextView title = (TextView) vi.findViewById(R.id.title);
		TextView description = (TextView) vi.findViewById(R.id.description1);
		TextView date = (TextView) vi.findViewById(R.id.date);
		image  = (ImageView) vi.findViewById(R.id.feedImage);
		//Log.d("news", " after textview" );
		
		Typeface myTypeFace = Farsi.GetFarsiFont(context);
		//Log.d("news", " after type face" );
		title.setTypeface(myTypeFace);
		description.setTypeface(myTypeFace);
		date.setTypeface(myTypeFace);
		//Log.d("news", " after setting typeface" );
		
		HashMap<String, String> news = new HashMap<String, String>();
		//Log.d("news", " after creating hashmap" );
		news = data.get(position);
		//Log.d("news", " getting data from data hashmap" );
		//Log.d("news", "" + news);
		// Setting all values in listview
		Log.d("Image Link", "" + news.get(MessageList.TAG_IMAGE));
		if (news.get(MessageList.TAG_IMAGE) != null && !news.get(MessageList.TAG_IMAGE).contains(".gif")) {
			Log.d("Image Link", news.get(MessageList.TAG_IMAGE));
			image.setImageResource(R.drawable.imageloading);
			imageLoader.DisplayImage(news.get(MessageList.TAG_IMAGE), image);
		}else
			image.setVisibility(View.GONE);
		
		
		title.setText(Farsi.Convert(news.get(MessageList.TAG_TITLE)));
		description.setText(Farsi.Convert(news.get(MessageList.TAG_DESCRIPTION)));
		date.setText(Farsi.Convert(news.get(MessageList.TAG_DATE)));
		
		return vi;
	}

}
